package com.spikegpsongoogleappengine.datasource;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.spikegpsongoogleappengine.CompanyInfo;

public class CompanyInfoDummyDataSource implements CompanyInfoDataSource {

	private static final int MAX_LAT = (int) (42.0200 * 1E6);
	private static final int MIN_LAT = (int) (42.0136 * 1E6);

	private static final int MIN_LON = (int) (21.356 * 1E6);
	private static final int MAX_LON = (int) (21.366 * 1E6);

	private static String[] fnOrgUrl = { "ruze Inc.", "Sinisa Trade", "Skrce negr",
			"firma4", "firma5" };
	private static String[] streetAddress = { "adresa 1", "adresa 2",
			"adresa 3", "adresa 4", "adresa 5" };
	private static String[] locality = { "grad 1", "grad 2", "grad 3",
			"grad 4", "grad 5" };
	private static String[] tel = { "070233444", "075339282", "077746726",
			"02 2434 299" };
	private static String[] classification = { "klasifikacija" };

	public static List<CompanyInfo> getRandomCompanyInfo(int numberOfInstances) {
		List<CompanyInfo> companyInfos = new ArrayList<CompanyInfo>();
		for (int i = 0; i < numberOfInstances; i++) {

			companyInfos.add(new CompanyInfo(1L, "", fnOrgUrl[getRandomInteger(
					0, fnOrgUrl.length - 1)], streetAddress[getRandomInteger(0,
					streetAddress.length - 1)], locality[getRandomInteger(0,
					locality.length - 1)],
					getRandomInteger(MIN_LAT, MAX_LAT) / 1E6F,
					getRandomInteger(MIN_LON, MAX_LON) / 1E6F,
					tel[getRandomInteger(0, tel.length - 1)],
					classification[getRandomInteger(0,
							classification.length - 1)], "", ""));
		}
		return companyInfos;
	}

	private static int getRandomInteger(int aStart, int aEnd) {
		Random aRandom = new Random();
		if (aStart > aEnd) {
			throw new IllegalArgumentException("Start cannot exceed End.");
		}
		// get the range, casting to long to avoid overflow problems
		long range = (long) aEnd - (long) aStart + 1;
		// compute a fraction of the range, 0 <= frac < range
		long fraction = (long) (range * aRandom.nextDouble());
		int randomNumber = (int) (fraction + aStart);
		return randomNumber;
	}

	@Override
	public List<CompanyInfo> getCompanies(String queryText, Double lat,
			Double lng) {
		return getRandomCompanyInfo(getRandomInteger(4, 5));

	}

}
